﻿using System;
using System.Collections.Generic;

namespace NetworkLib.Utilities
{
    public class DataByteArrayList<T> : DataByteArray, IEnumerable<T> where T : DataByteArray
    {
        protected T[] elements;

        public DataByteArrayList()
        {
            this.elements = new T[0];
        }

        public DataByteArrayList(T[] array)
        {
            this.elements = array;
        }

        public override void Read(ByteArray byteArray)
        {
            this.elements = DataByteArray.ReadArray<T>(byteArray);
        }

        public override void Write(ByteArray byteArray)
        {
            DataByteArray.WriteArray<T>(this.elements, byteArray);
        }

        #region IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            foreach (var item in this.elements)
            {
                yield return item;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.elements.GetEnumerator();
        }

        #endregion

    }
}
